﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Devfw.OpenAuth
{
    public class ParameterCollection : List<Parameter>
    {
        public ParameterCollection()
            : base()
        {
        }

        public ParameterCollection( IEnumerable<Parameter> e )
            : base( e )
        {
        }

        public Parameter[] this[string parameterName]
        {
            get
            {
                return this.AsEnumerable().Where( p => p.Name == parameterName ).ToArray();
            }
        }

        public void Add( string name, string value, ParameterType type )
        {
            this.Add( new Parameter( name, value, type ) );
        }

        public Parameter[] GetParameters( ParameterType type )
        {
            return this.AsEnumerable().Where( p => p.Type == type ).ToArray();
        }

        /// <summary>
        /// see Normalize Request Parameters Sort part at http://oauth.net/core/1.0/#rfc.section.9.1.1
        /// </summary>
        public new void Sort()
        {
            this.Sort( new ParameterComparer() );
        }

        public static ParameterCollection ParseQueryString( string query )
        {
            ParameterCollection p = new ParameterCollection();
            var nvc = System.Web.HttpUtility.ParseQueryString( query );
            foreach( string key in nvc.Keys )
            {
                p.Add( key, nvc[key], ParameterType.Get );
            }
            return p;
        }
    }
}
